// Javascript

function doCRUDLine(id, quote_id){
    window.location = '?m=quotations&a=line_crud&quote_id='+quote_id+'&tab=1';
    //var param = 'line_id='+id+''+extra;
    //ajax.send('?m=quotations&a=line_crud&suppressHeaders=1', param,
		//function (r) {
			//var cd = Control('idCRUDQuotations').create('div', 'popup', 'hidden');
			//cd.innerHTML = r;
			//Dialog(cd, closeReference).show();
		//}
	//);
}

function doCRUDFile(id, extra){
    var param = 'file_id='+id+''+extra;
    ajax.send('?m=quotations&a=file_crud&suppressHeaders=1', param,
		function(r) {
			$('idCRUDFileDialog').innerHTML = r;
			Dialog('idCRUDFileDialog', closeCRUD('idCRUDFile')).show();
			//var cd = Control('idCRUDFile').create('div', 'popup', 'hidden');
			//cd.innerHTML = r;
			//Dialog(cd, closeCRUD('idCRUDFile')).show();
		}
	);
}

function deleteQuotation(name, id){
    if (confirm( "Do you want to delete "+ name)) {
		$('del_cc_id').value = id;
		f = $('MdFormDelete');
		f.del.value = id;
		f.submit();
	}
}

function doSearchQuotation(frm, keyName, actId){
    var keyVal = frm.elements[keyName].value;
    if((keyVal.trim()).length > 0){
        $(actId).remove();
        frm.submit();
        return true;
    }
    return false;
}

function doQuotationActive(frm, quoteId, code, name, start_date, expire_date){
	if(start_date==''||expire_date==''){
		alert('Please input information start_date and expire_date');
		return;
	}
	var result = confirm('Do you want to active [' + code + '] ' + name + '?');
    if(result){
    	var param = 'statusActive=1&quote_id=' + quoteId;
    	param += '&quote_start_date=' + start_date;
    	param += '&quote_expire_date=' + expire_date;
        ajax.send('?m=quotations&a=do_quote_crud&suppressHeaders=1', param,
    		function(r) {
                if(r=='successful'){
                    history.go(0);
                }
                else{
                    alert('Update fail: ' + r);
                }
    		}
    	);
    }
    return result;
}

function doQuotationComplete(frm, quoteId, code, name){
	var result = confirm('Do you want to complete [' + code + '] ' + name + '?');
    if(result){
    	var param = 'statusComplete=1&quote_id=' + quoteId;
        ajax.send('?m=quotations&a=do_quote_crud&suppressHeaders=1', param,
    		function(r) {
                if(r=='successful'){
                    history.go(0);
                }
                else{
                    alert('Update fail: ' + r);
                }
    		}
    	);
    }
    return result;
}

function doQuotationUnActive(frm, quoteId, code, name){
	var result = confirm('Do you want to un_active [' + code + '] ' + name + '?');
    if(result){
    	var param = 'statusUnActive=1&quote_id=' + quoteId;
        ajax.send('?m=quotations&a=do_quote_crud&suppressHeaders=1', param,
    		function(r) {
                if(r=='successful'){
                    history.go(0);
                }
                else{
                    alert('Update fail: ' + r);
                }
    		}
    	);
    }
    return result;
}

function deleteQuotationLine(name, id){
    // Show dialog
    var result = confirm('Are you sure to delete ' + name + '?');
    if(result){
//        var param = 'del=1&item_id='+id;
//        ajax.send('?m=quotations&a=do_line_crud&suppressHeaders=1', param,
//    		function(r) {
//    			var cd = Control('idCRUDQuotations').create('div', 'popup', 'hidden');
    			// Delete a row in table
//    			cd.innerHTML = r;
//    			Dialog(cd, closeReference).show();
//                if(r=='true'){
//                    history.go(0);
//                }
//                else{
                    // Alert
//                    alert('Delete fail');
//                }
//    		}
//    	);
    }
    else{
    }
    return result;
}

function deleteQuotationFile(name, id){
    // Show dialog
    var result = confirm('Are you sure to delete ' + name + '?');
    if(result){
        var param = 'del=1&quote_id='+id;
        ajax.send('?m=quotations&a=do_quote_crud&suppressHeaders=1', param,
    		function(r) {
//    			var cd = Control('idCRUDQuotations').create('div', 'popup', 'hidden');
    			// Delete a row in table
//    			cd.innerHTML = r;
//    			Dialog(cd, closeReference).show();
                if(r=='true'){
                    history.go(0);
                }
                else{
                    // Alert
                    alert('Delete fail');
                }
    		}
    	);
    }
    else{
        return result;
    }
}

function generateActiveStatus(){
	Dialog('idActiveStatusDialog', closeCRUD('idActiveStatus')).show();
}

function closeCRUD(id){
    Control(id).destroy();
	Dialog(id).hide();
}

//
function doCancel(url){
	if(confirm('Do you want to cancel')){
		window.location = url;
	}
	return false;
}

function changeApplyMode(id_hidden, id_show){
	var hidden = document.getElementById(id_hidden);
	var show = document.getElementById(id_show);

	hidden.style.display = 'none';
	show.style.display = '';
}